package main;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import common.AntiCrack;

import form.CommonForm;

public class FrameSet implements ActionListener {

	JFrame jf;
	Frame frame;
	getLocation glcate;
	JLabel jl11, jl13, jl21, jl3;
	JPanel jp1, jp2, jp3, jp4;
	JButton jb31, jb32, jb33, jb34, jb41, jb42, jb44;
	JTextField jtf, jtf12, jtf14, jtf22;
	CommonForm cf;

	public FrameSet(final CommonForm cf) {
		try {
			glcate = new getLocation();
			jf = new JFrame("设置");
			jf.setBounds(0, 0, 300, 225);
			jf.setResizable(false);
			jf.setLayout(new GridLayout(4, 1));
			this.cf = cf;
			GridLayout gl = new GridLayout(1, 4);
			jl11 = new JLabel("切换间隔ms");
			jl11.setOpaque(true);
			jl11.setBackground(Color.lightGray);
			jl13 = new JLabel("按键间隔ms");
			jl13.setOpaque(true);
			jl13.setBackground(Color.lightGray);
			jp1 = new JPanel();
			jp1.setLayout(gl);
			jp2 = new JPanel();
			jp2.setLayout(new GridLayout(1, 2));
			jl21 = new JLabel("顺序切换延迟时间(s):", JLabel.RIGHT);
			jtf22 = new JTextField();
			jtf22.addKeyListener(new KeyListener() {
				public void keyTyped(KeyEvent e) {
					if (e.getKeyChar() == '\n') {
						try {
							cf.setTime3((int) (Double.parseDouble(jtf22
									.getText()) * 1000));
						} catch (Exception e1) {
							cf.setTime3(3000);
							jtf22.setText("3");
						}
						jtf22.setBackground(Color.GREEN);
					}
				}

				public void keyReleased(KeyEvent e) {
				}

				public void keyPressed(KeyEvent e) {
				}
			});
			jp2.add(jl21);
			jp2.add(jtf22);
			jp3 = new JPanel();
			jp3.setLayout(new GridLayout(1, 1));
			jl3 = new JLabel("自动按左右键 & 保存↓", JLabel.CENTER);
			jl3.setOpaque(true);
			jl3.setBackground(Color.lightGray);
			jp3.add(jl3);
			jp4 = new JPanel();
			jp4.setLayout(gl);
			jb41 = new JButton("左键");
			jb41.setActionCommand("jb41");
			jb42 = new JButton("右键");
			jb42.setActionCommand("jb42");
			jb44 = new JButton("保存");
			jb44.setActionCommand("save");
			jb41.addActionListener(this);
			jb42.addActionListener(this);
			jb44.addActionListener(this);
			jtf = new JTextField();
			jtf12 = new JTextField();
			jtf12.addKeyListener(new KeyListener() {
				public void keyTyped(KeyEvent e) {
					if (e.getKeyChar() == '\n') {
						try {
							cf.setTime1(Integer.parseInt(jtf12.getText()));
						} catch (Exception e1) {
							cf.setTime1(50);
							jtf12.setText("50");
						}
						jtf12.setBackground(Color.GREEN);
					}
				}

				public void keyReleased(KeyEvent e) {
				}

				public void keyPressed(KeyEvent e) {
				}
			});
			jtf14 = new JTextField();
			jtf14.addKeyListener(new KeyListener() {
				public void keyTyped(KeyEvent e) {
					if (e.getKeyChar() == '\n') {
						try {
							cf.setTime2(Integer.parseInt(jtf14.getText()));
						} catch (Exception e1) {
							cf.setTime2(10);
							jtf14.setText("10");
						}
						jtf14.setBackground(Color.GREEN);
					}
				}

				public void keyReleased(KeyEvent e) {
				}

				public void keyPressed(KeyEvent e) {
				}
			});
			jp1.add(jl11);
			jp1.add(jtf12);
			jp1.add(jl13);
			jp1.add(jtf14);
			jp4.add(jb41);
			jp4.add(jtf);
			jp4.add(jb42);
			jp4.add(jb44);
			jf.add(jp1);
			jf.add(jp2);
			jf.add(jp3);
			jf.add(jp4);
			jf.addWindowListener(new WindowListener() {
				public void windowOpened(WindowEvent e) {
				}

				public void windowIconified(WindowEvent e) {
				}

				public void windowDeiconified(WindowEvent e) {
				}

				public void windowDeactivated(WindowEvent e) {
				}

				public void windowClosing(WindowEvent e) {
					jf.setVisible(false);
				}

				public void windowClosed(WindowEvent e) {
				}

				public void windowActivated(WindowEvent e) {
				}
			});
		} catch (Exception e) {
			IllegalArgumentException illegalargumentexception = new IllegalArgumentException(
					e.toString());
			AntiCrack.Fake(illegalargumentexception, e);
			throw illegalargumentexception;
		}
	}

	public void show(Frame frame, String title) {
		// TODO show
		try {
			jf.setTitle(title);
			this.frame = frame;
			jf.setVisible(true);
		} catch (Exception e) {
			IllegalArgumentException illegalargumentexception = new IllegalArgumentException(
					e.toString());
			AntiCrack.Fake(illegalargumentexception, e);
			throw illegalargumentexception;
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			if (e.getActionCommand().equals("save")) {
				if(JOptionPane.showConfirmDialog(null, "确定保存信息？")==JOptionPane.YES_OPTION)
				frame.save();
			} else if (e.getActionCommand().equals("jb41")) {
				try {
					glcate.PressKey(1, Integer.parseInt(jtf.getText()));
				} catch (Exception e1) {
					glcate.PressKey(1, 1);
				}
			} else if (e.getActionCommand().equals("jb42")) {
				try {
					glcate.PressKey(2, Integer.parseInt(jtf.getText()));
				} catch (Exception e1) {
					glcate.PressKey(2, 1);
				}
			}
		} catch (Exception e1) {
			IllegalArgumentException illegalargumentexception = new IllegalArgumentException(
					e.toString());
			AntiCrack.Fake(illegalargumentexception, e1);
			throw illegalargumentexception;
		}
	}

	public void getTime() {
		try {
			try {
				jtf12.setText(cf.getTime1() + "");
			} catch (Exception e) {
				jtf12.setText("50");
				cf.setTime1(50);
			}

			try {
				jtf14.setText(cf.getTime2() + "");
			} catch (Exception e) {
				jtf14.setText("10");
				cf.setTime2(10);
			}

			try {
				jtf22.setText(cf.getTime3() / 1000 + "");
			} catch (Exception e) {
				jtf22.setText("3");
				cf.setTime3(3000);
			}
		} catch (Exception e) {
			IllegalArgumentException illegalargumentexception = new IllegalArgumentException(
					e.toString());
			AntiCrack.Fake(illegalargumentexception, e);
			throw illegalargumentexception;
		}
	}

}
